package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.enums.vehicle.RechargeQueryEnum;
import com.xhwl.common.enums.vehicle.RechargeTypeEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.vehicle.VehcPayRecord;
import com.xhwl.common.query.cent.vehicle.PayRecordQuery;
import com.xhwl.logistics.dao.VehcPayRecordDao;
import com.xhwl.logistics.service.IVehcPayRecordService;
import com.xhwl.logistics.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lu.Aaron
 * @since 2021-08-16
 */
//@Service
public class VehcPayRecordServiceImpl extends ServiceImpl<VehcPayRecordDao, VehcPayRecord> implements IVehcPayRecordService {
    @Autowired
    private VehcPayRecordDao vehcPayRecordDao;
    @Override
    public ResultJson payRecordPage(PayRecordQuery payRecordQuery, Account account) {
        if (account.getId() == null) {
            throw new BusinessException("请先完成登录");
        }
        Date endTime = payRecordQuery.getEndTime();
        Date startTime = payRecordQuery.getStartTime();
        Integer projectId = payRecordQuery.getProjectId();
        RechargeQueryEnum rechargeQuery = payRecordQuery.getRechargeQuery();
        String plateNumber = payRecordQuery.getPlateNumber();
        Long size = payRecordQuery.getSize();
        Long current = payRecordQuery.getCurrent();

        if (null==size) {
            size = 20L;
        }
        if (null==current) {
            current = 1L;
        }

        QueryWrapper<VehcPayRecord> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(endTime)) {
            wrapper.le("pay_time",endTime);
        }
        if (!Objects.isNull(startTime)) {
            wrapper.ge("pay_time",startTime);
        }
        if (!Objects.isNull(projectId)) {
            wrapper.eq("project_id",projectId);
        }
        if (StringUtils.isNotBlank(plateNumber)) {
            wrapper.like("plate_number",plateNumber);
        }
        if (!Objects.isNull(rechargeQuery)) {
            if (RechargeQueryEnum.TEMP_PARK_RECHARGE.equals(rechargeQuery)) {
                wrapper.eq("recharge_type", RechargeTypeEnum.TEMP_PARK_RECHARGE.getKey());
            }else if(RechargeQueryEnum.RECHARGE.equals(rechargeQuery)){
                wrapper.in("recharge_type", RechargeTypeEnum.MONTH_CARD_RECHARGE.getKey(),RechargeTypeEnum.STORE_CARD_RECHARGE.getKey());
            }
        }
        wrapper.eq("payer_account_id",account.getId());
        Page<VehcPayRecord> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        Page<VehcPayRecord> payRecordPage = vehcPayRecordDao.selectPage(page, wrapper);
        return ResultJson.success(payRecordPage);
    }

    @Override
    public ResultJson recently(Account account) {
        if (account.getId() == null) {
            throw new BusinessException("请先完成登录");
        }
        List<VehcPayRecord> vehcPayRecords = vehcPayRecordDao.selectList(new QueryWrapper<VehcPayRecord>().eq("payer_account_id", account.getId()).orderByDesc("pay_time"));
        return ResultJson.success(vehcPayRecords);
    }
}
